#! /bin/bash
#
# System Report
# Copyright (c) 1999-2003 Red Hat, Inc. All rights reserved.
#
# Written by:
#     Wil Harris <wil@redhat.com>
#     Peter Jones <pjones@redhat.com>
#     Preston Brown <pbrown@redhat.com>
#     Than Ngo <than@redhat.com>

# adapted by P.Dorgueil <pierre.dorgueil@free.fr> from
#
#Name        : sysreport                    Relocations: (not relocateable)
#Version     : 1.3.5                             Vendor: Red Hat, Inc.
#Release     : 2                             Build Date: Thu Jul 31 18:56:57 2003
#Install Date: (not installed)               Build Host: dolly.devel.redhat.com
#Group       : Development/Debuggers         Source RPM: sysreport-1.3.5-2.src.rpm
#Size        : 17531                            License: GPL
#Signature   : DSA/SHA1, Thu Aug 21 20:36:20 2003, Key ID 94cd5742e418e3aa
#Packager    : Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla>
#URL         : http://www.redhat.com/support
#Summary     : Gathers system hardware and configuration information.
#Description :
#Sysreport is a utility that gathers information about a system's
#hardware and configuration. The information can then be used for
#diagnostic purposes and debugging. Sysreport is commonly used to help
#support technicians and developers by providing a "snapshot" of a
#system's current layout.
#
# only hardware-related infos were kept.


# functions


catiffile() {
  res=1
  file=$1; shift; echo -n $1
  if [ -e $file ]; then
   dir=$(dirname $file)
   name=$(basename $file)
   cd $dir
   x="find $name -print"
   [ "$2" = "" ] && list=$($x) || list=$($x | grep -v $2)
   res=0
   for i in $list; do
    if [ -d $i ]; then mkdir -p $ROOTDIR/$i || res=2; fi
    if [ -f $i ]; then cp -a $i $ROOTDIR/$i || res=1; fi
   done
  fi
  if [ $res -eq 0 ]; then
   ln -s $name $ROOTDIR/$1
   echo_success
  else
   echo_failure
  fi
  return $res
}

catifexec() {
  res=1
  file=$1; shift; echo -n $1
  if [ -x $file ]; then
    name=$(basename $file)
    dest=$ROOTDIR/$name
    $file > $dest 2>&1 && ln -s $name $ROOTDIR/$1
    res=$?
  fi
  [ $res -eq 0 ] && echo_success || echo_failure
  return $res
}

catiffunc() {
  res=1
  file=$1; shift; echo -n $1
  $file > $ROOTDIR/$file 2>&1 && ln -s $file $ROOTDIR/$1
  res=$?
  [ $res -eq 0 ] && echo_success || echo_failure
  return $res
}

echo_() {
  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  echo -n "[  "
  [ "$BOOTUP" = "color" ] && $1
  echo -n $2
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "  ]"
}
echo_success() {
 echo_ "$SETCOLOR_SUCCESS" " OK "
}
echo_passed()  {
 echo_ "$SETCOLOR_WARNING" "WARN"
}
echo_failure() {
 echo_ "$SETCOLOR_FAILURE" "FAIL"
}


BOOTUP=color
MOVE_TO_COL="echo -en \\033[20G"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \\033[0;39m"
LOGLEVEL=1


##################################################

umask 0077
ROOTBASE=/tmp
ROOTNAME=sysreport.`date +%Y%m%d.%H%M%S`
ARCHNAME=$ROOTNAME.tbz
ROOTDIR=$ROOTBASE/$ROOTNAME
LOG=$ROOTBASE/Log

PATH="/bin:/usr/bin"

rm -rf $ROOTDIR
mkdir $ROOTDIR

quiet=n; [ $# -gt 0 ] && quiet=y

(
 uname_a() {
  uname -a
  grep -i version /etc/motd
 }
 fdisk_l() {
  /sbin/fdisk -l
 }
 echo "Collecting system infos..."
 catifexec /bin/date           Sys-Date
 catifexec /usr/bin/uptime     Sys-Uptime
 catiffunc uname_a             Sys-Name
 catiffunc fdisk_l             Sys-Part
 catiffile /proc/stat          Mbd-Stat
 catiffile /proc/cpuinfo       Mbd-Cpu
 catiffile /proc/meminfo       Mbd-Ram
 catiffile /proc/interrupts    Mbd-Irqs
 catiffile /proc/dma           Mbd-Dma
 catiffile /proc/ioports       Mbd-Ports
 catiffile /proc/pci           Bus-Pci
 catiffile /proc/scsi          Bus-Scsi
 catiffile /proc/ide           Bus-Ide        smart_
 catifexec /bin/dmesg          Krn-Boot
 catiffile /proc/sys           Krn-Sys        route/flush
 catifexec /sbin/lsmod         Krn-Mods
 catiffile /proc/devices       Krn-Devs
 catifexec /usr/bin/free       Krn-Free
 catiffile /proc/filesystems   Krn-Fs
 catiffile /proc/partitions    Krn-Part
 catiffile /proc/mdstat        Krn-Raid
 catiffile /var/log            Krn-Logs
) | ( [ $quiet = n ] && tee $LOG || cat > $LOG )

if [ $quiet = n ]; then
 LINE=x
 echo 
 echo "Any useful comment here : email, machine brand name, bug description, ..."
 echo "End with an empty line"
 while [ "$LINE" != "" ]; do    
  read LINE
  echo "$LINE" >> $LOG
 done
fi

cd $ROOTBASE
tar cvvf $ARCHNAME $ROOTNAME >>$LOG 2>/dev/null
tar cjf  $ARCHNAME $ROOTNAME >/dev/null 2>&1
rm -Rf $ROOTDIR


if [ $quiet = n ]; then
 echo 
 echo "Results in $ROOTBASE/$ARCHNAME"
 echo "If you want a diskette copy of this file, please put a"
 echo "msdos formatted diskette in the drive and press <Enter>"
 read OK

 FLOP=/mnt/floppy
 DEST1=$FLOP/report1.tbz
 DEST2=$FLOP/report2.tbz
 mkdir -p $FLOP && \
 mount /dev/fd0 $FLOP && \
  cp $ARCHNAME $DEST1 && cp $ARCHNAME $DEST2
 umount $FLOP

 mount /dev/fd0 $FLOP
 if cmp $ARCHNAME $DEST1; then
  if cmp $ARCHNAME $DEST2; then
   echo "Done; you may send one of the two report files by mail"
   umount $FLOP
   exit 0
  fi
 fi
 umount $FLOP
 echo "Sorry: an error occured during report file copying."
 exit 1
else
 echo $ROOTBASE/$ARCHNAME
 exit 0
fi

